package com.hsd.znsh.security;

import com.hsd.znsh.properties.SecurityConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.PortMapper;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-23 09:48:08
 */
@Component
@Data
@EqualsAndHashCode(callSuper = false)
public class MyAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint{

    private Map<String, String> authEntryPointMap =new HashMap<>();

    private PathMatcher pathMatcher = new AntPathMatcher();

    public MyAuthenticationEntryPoint(){
        super(SecurityConstants.DEFAULT_PC_LOGIN_PROCESSING_URL);
        //authEntryPointMap.put("/**/app/**","/app/user/login");
        authEntryPointMap.put("/**/manager/**","/manager/user/login");
        authEntryPointMap.put("/**/pc/**","/pc/user/login");
    }

    @Override
    protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
        String requestURI = request.getRequestURI().replace(
                request.getContextPath(), "");
        for (String url : this.authEntryPointMap.keySet()) {
            if (this.pathMatcher.match(url, requestURI)) {
                return this.authEntryPointMap.get(url);
            }
        }
        return super.determineUrlToUseForThisRequest(request, response, exception);
    }


}
